Skip to content

Make concurrent_writer::continue_writing_ atomic (fixes #3464) - #3465

Open
viftode4 wants to merge 1 commit into
stan-dev:developfrom
viftode4:fix-concurrent-writer-flag-race
Open

viftode4 wants to merge 1 commit into
stan-dev:developfrom
viftode4:fix-concurrent-writer-flag-race

Conversation

@viftode4

Copy link
Copy Markdown

Summary

Make concurrent_writer::continue_writing_ a std::atomic<bool>. Fixes #3464.

Intended Effect

wait() writes the flag from the calling thread while the worker reads it in its loop condition. That is a data race under the C++ memory model, and ThreadSanitizer reports it for a writer that is constructed and then waited on. The change removes the report. Behaviour is otherwise unchanged. wait() still sets the flag and then joins. The worker loop is unchanged.

On @WardBrian's question in the issue: on x86 I do not expect a visible failure. The worker's loop has opaque calls (try_pop, yield), so the compiler is unlikely to hoist the load. The race is formally undefined behaviour, and it makes TSan builds of any Stan program using threaded output fail, so a one-line atomic seemed cheap. Close this if you would rather not take it.

How to Verify

Reporter's program from #3464 (construct a concurrent_writer, call wait()), built with -O1 -g -fsanitize=thread -DSTAN_THREADS, GCC 13.3, Ubuntu 24.04 x86-64, unmodified develop against this branch, 20 runs each:

  • before: data race reported 20/20
  • after: 0/20

I did not run the repo's unit-test suite or make cpplint.

Side Effects

None expected. Default seq_cst loads and stores on a bool flag.

Documentation

None.

Copyright and Licensing

Copyright holder: viftode4 (the account submitting this).

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses:

AI disclosure

This change and the TSan check were made by an AI agent (Claude, running as an inhabitant of the SLIME project) working for the account owner. The reporter's reproduction was reused as is.

🤖 Generated with Claude Code

wait() writes the flag from the calling thread while the worker thread reads it in its loop condition. ThreadSanitizer reports this as a data race on an idle writer.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@WardBrian

Copy link
Copy Markdown
Member

@SteveBronder while adding the pathfinder concurrent writer in #3325 were there any specific benchmarks you were running that would be good to re-run with this change?

@WardBrian

Copy link
Copy Markdown
Member

@viftode4 Thanks for the PR. Would you mind removing the co-authored-by bit on the commit? Our AI policy requires the human contributor to take responsibility for the patch (which seems easy, in this case)

@bob-carpenter

Copy link
Copy Markdown
Member

@viftode4: Sorry about this, but we've been hashing out our AI policy. None of this is about your PR per se.

@WardBrian If our policy is going to be to forbid Claude attributions in headers, maybe we could cut them off at the pass by blocking commits that are by "X and Claude". Or maybe some kind of hook in PRs that looks for this boilerplate?

I don't know if there's a way to get commits made by Claude to not include the self attribution, but if so, we should indicate that to users if this is our policy by pointing them to Claude's doc.

It would be great if these automatic rejections could point to an explicit piece of our AI policy. It's not clear to me, at least, that this PR violates our policies.

@bob-carpenter

Copy link
Copy Markdown
Member

Another thing to flag in this PR is the copyright. Are we running into legal issues accepting PRs attributed to GitHub accounts? That's not how copyright works. It gets assigned to the person who authored the content or to whatever legal entity they assign their copyright to. I do not believe GitHub accounts are legitimate copyright holders. This is not the only case of this we have as not all of our developers have identified themselves "in real life."

@stan-buildbot

Copy link
Copy Markdown
Contributor
Name Old Result New Result Ratio Performance change( 1 - new / old )
stat_comp_benchmarks/benchmarks/gp_regr/gen_gp_data.stan 0.04 0.04 1.02 1.95% faster
stat_comp_benchmarks/benchmarks/gp_regr/gp_regr.stan 0.18 0.18 1.0 -0.03% slower
stat_comp_benchmarks/benchmarks/garch/garch.stan 0.96 0.96 1.0 0.26% faster
stat_comp_benchmarks/benchmarks/low_dim_corr_gauss/low_dim_corr_gauss.stan 0.01 0.01 0.98 -2.19% slower
stat_comp_benchmarks/benchmarks/eight_schools/eight_schools.stan 0.11 0.11 1.01 0.69% faster
stat_comp_benchmarks/benchmarks/low_dim_gauss_mix/low_dim_gauss_mix.stan 8.05 8.11 0.99 -0.65% slower
stat_comp_benchmarks/benchmarks/gp_pois_regr/gp_pois_regr.stan 4.03 4.04 1.0 -0.33% slower
stat_comp_benchmarks/benchmarks/pkpd/one_comp_mm_elim_abs.stan 33.25 32.56 1.02 2.09% faster
stat_comp_benchmarks/benchmarks/pkpd/sim_one_comp_mm_elim_abs.stan 0.38 0.38 1.0 -0.0% slower
stat_comp_benchmarks/benchmarks/irt_2pl/irt_2pl.stan 7.94 7.94 1.0 0.09% faster
stat_comp_benchmarks/benchmarks/sir/sir.stan 166.75 165.18 1.01 0.94% faster
stat_comp_benchmarks/benchmarks/arK/arK.stan 3.45 3.45 1.0 -0.1% slower
stat_comp_benchmarks/benchmarks/arma/arma.stan 0.58 0.58 0.99 -0.76% slower
stat_comp_benchmarks/benchmarks/low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 25.14 25.17 1.0 -0.12% slower
performance.compilation 403.11 396.68 1.02 1.59% faster
Mean result: 1.0024178021510741

Jenkins Console Log
Jenkins Build Stages
Commit hash: 0ed527b361ff570d4ef4fd7c95e47dca0f433fa3

Machine information
Distributor ID:	Ubuntu
Description:	Ubuntu 20.04.3 LTS
Release:	20.04
Codename:	focal

CPU:

Architecture:                            x86_64
CPU op-mode(s):                          32-bit, 64-bit
Byte Order:                              Little Endian
Address sizes:                           52 bits physical, 57 bits virtual
CPU(s):                                  192
On-line CPU(s) list:                     0-191
Thread(s) per core:                      2
Core(s) per socket:                      48
Socket(s):                               2
NUMA node(s):                            2
Vendor ID:                               AuthenticAMD
CPU family:                              25
Model:                                   17
Model name:                              AMD EPYC 9474F 48-Core Processor
Stepping:                                1
Frequency boost:                         enabled
CPU MHz:                                 1497.373
CPU max MHz:                             4114.4229
CPU min MHz:                             1500.0000
BogoMIPS:                                7189.43
Virtualization:                          AMD-V
L1d cache:                               3 MiB
L1i cache:                               3 MiB
L2 cache:                                96 MiB
L3 cache:                                512 MiB
NUMA node0 CPU(s):                       0-47,96-143
NUMA node1 CPU(s):                       48-95,144-191
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Old microcode:             Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Mitigation; Safe RET
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; Enhanced / Automatic IBRS; IBPB conditional; STIBP always-on; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Mitigation; Clear CPU buffers
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Mitigation; IBPB before exit to userspace
Flags:                                   fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl xtopology nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d debug_swap

G++:

g++ (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Clang:

clang version 10.0.0-4ubuntu1 
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ThreadSanitizer reports a shutdown race in concurrent_writer

4 participants